// const modules = import.meta.glob("../components/**/*.vue")
const deepCopy = (value: any) => {
  if (value instanceof Function) return value
  else if (value instanceof Array) {
    const newValue = [] as any
    for (let i = 0; i < value.length; ++i) newValue[i] = deepCopy(value[i])
    return newValue
  } else if (value instanceof Object) {
    const nnewValue = {} as any
    for (const i in value) nnewValue[i] = deepCopy(value[i])
    return nnewValue
  } else return value
}
const createRoute = (allRoutes: any) => {
  const s = deepCopy(allRoutes)
  const newrouter: any[] = []
  s.forEach((e: any) => {
    let obj
    if (e.desc) {
      obj = {
        path: `/${e.path}`,
        name: e.path,
        meta: {
          name: e.name,
          hidden: e.hidden,
          icon: e.icon,
        },
        component: () => import(/* @vite-ignore */ `@/components/${e.desc}.vue`),
      }
    } else {
      obj = {
        path: `/${e.path}`,
        name: e.path,
        meta: {
          name: e.name,
          hidden: e.hidden,
          icon: e.icon,
        },
        redirect: `/${e.children[0].path}`,
        children: createRoute(e.children),
      }
    }
    newrouter.push(obj)
  })
  console.log("====================================")
  console.log("targeta", newrouter)
  console.log("====================================")
  return newrouter
}

const createRouter = (allRoutes: any, mockRouterData: any) => {
  console.log(allRoutes,'allRoutes')
  console.log(mockRouterData,'mockRouterData')

  const s = deepCopy(allRoutes)
  const newrouter: any[] = []
  s.forEach((e: any) => {
    mockRouterData.forEach((es: any) => {
      if (e.path == es) {
        if (e.children && e.children.length > 0) {
          e.children = createRouter(e.children, mockRouterData)
        }
        newrouter.push(e)
      }
    })
  })
  console.log("====================================")
  console.log("targetb", newrouter)
  console.log("====================================")
  return newrouter
}

export { createRouter, createRoute }
